package com.zenchn.controller;

import com.zenchn.entity.HttpResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 错误信息控制器
 *
 * @author panbingqi
 * @date 2021-06-01
 **/
@RestController
@RequestMapping("error")
@Api(value = "请求出错时，相关跳转接口",description="请求出错时，相关跳转接口")
@ApiIgnore()
public class ErrorController {

    @ApiOperation(value = "未授权-当前账号无法使用该功能.", notes = "未授权-当前账号无法使用该功能.")
    @RequestMapping(value = "/401")
    public ResponseEntity<HttpResult> error401(HttpServletRequest request) {
        HttpResult result = new HttpResult(request.getRequestURI());
        result.setMessage("未授权-当前账号无法使用该功能.");
        result.setStatus(HttpStatus.UNAUTHORIZED.value());
        result.setError(HttpStatus.UNAUTHORIZED.getReasonPhrase());
        return new ResponseEntity<HttpResult>(result, HttpStatus.UNAUTHORIZED);
    }

    /**
     * 未登录，shiro应重定向到登录界面，此处返回未登录状态信息由前端控制跳转页面
     * @return
     */
    @ApiOperation(value = "未登录接口", notes = "未登录，shiro应重定向到登录界面，此处返回未登录状态信息由前端控制跳转页面")
    @RequestMapping(value = "/unlogin")
    public ResponseEntity<HttpResult> unLogin(HttpServletRequest request, HttpServletResponse response) {
        HttpResult result = new HttpResult(request.getRequestURI());
        result.setMessage("未登录.");
        result.setStatus(HttpStatus.UNAUTHORIZED.value());
        result.setError(HttpStatus.UNAUTHORIZED.getReasonPhrase());
        return new ResponseEntity<HttpResult>(result, HttpStatus.UNAUTHORIZED);
    }

}